//! 宏定义模块
//! 提供 uni_api 属性宏的实现

use proc_macro::TokenStream;
use syn::{parse_macro_input, AttributeArgs, ItemFn, Meta, NestedMeta};
use quote::quote;

/// 统一API属性宏
/// 
/// 用法:
/// ```rust
/// #[uni_api(route="/api/users", method="GET", auth_policy="role:admin")]
/// async fn get_users() -> Result<Json<Vec<User>>, Error> {
///     // 处理逻辑
/// }
/// ```
#[proc_macro_attribute]
pub fn uni_api(args: TokenStream, input: TokenStream) -> TokenStream {
    let attr_args = parse_macro_input!(args as AttributeArgs);
    let input_fn = parse_macro_input!(input as ItemFn);

    // 解析属性参数
    let mut route = None;
    let mut method = None;
    let mut auth_policy = None;
    let mut description = None;

    for arg in attr_args {
        match arg {
            NestedMeta::Meta(Meta::NameValue(nv)) => {
                if nv.path.is_ident("route") {
                    if let syn::Expr::Lit(syn::ExprLit { 
                        lit: syn::Lit::Str(lit_str), .. 
                    }) = nv.value {
                        route = Some(lit_str.value());
                    }
                } else if nv.path.is_ident("method") {
                    if let syn::Expr::Lit(syn::ExprLit { 
                        lit: syn::Lit::Str(lit_str), .. 
                    }) = nv.value {
                        method = Some(lit_str.value());
                    }
                } else if nv.path.is_ident("auth_policy") {
                    if let syn::Expr::Lit(syn::ExprLit { 
                        lit: syn::Lit::Str(lit_str), .. 
                    }) = nv.value {
                        auth_policy = Some(lit_str.value());
                    }
                } else if nv.path.is_ident("description") {
                    if let syn::Expr::Lit(syn::ExprLit { 
                        lit: syn::Lit::Str(lit_str), .. 
                    }) = nv.value {
                        description = Some(lit_str.value());
                    }
                }
            }
            _ => {}
        }
    }

    let fn_name = &input_fn.sig.ident;
    let fn_block = &input_fn.block;
    let fn_generics = &input_fn.sig.generics;
    let fn_inputs = &input_fn.sig.inputs;
    let fn_output = &input_fn.sig.output;

    // 生成文档注释
    let doc_comment = if let Some(desc) = description {
        quote! {
            #[doc = #desc]
        }
    } else {
        quote! {}
    };

    // 生成路由信息注释
    let route_comment = if let (Some(route_path), Some(http_method)) = (route, method) {
        quote! {
            #[doc = ""]
            #[doc = "Route: "]
            #[doc = #route_path]
            #[doc = ""]
            #[doc = "Method: "]
            #[doc = #http_method]
        }
    } else {
        quote! {}
    };

    // 生成认证注释
    let auth_comment = if let Some(policy) = auth_policy {
        quote! {
            #[doc = ""]
            #[doc = "Auth Policy: "]
            #[doc = #policy]
        }
    } else {
        quote! {}
    };

    let expanded = quote! {
        #doc_comment
        #route_comment
        #auth_comment
        pub #fn_generics #fn_name #fn_inputs #fn_output {
            #fn_block
        }
    };

    TokenStream::from(expanded)
}